package com.cspg.design.ui.project;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cspg.design.CSPGApplication;
import com.cspg.design.R;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.bean.projectBean.ProjectBean;
import com.cspg.design.dao.gen.ProjectBeanDao;
import com.cspg.design.presenter.GTaskPresenter;
import com.cspg.design.ui.gtasks.GTasksActivity;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.widget.AlertListDialog;
import com.cspg.design.widget.ToastUtils;

public class ProjectInfoActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tvProjectName;
    private TextView tvProjectUnit;
    private TextView etProjectCode;
    private EditText etProjectInvestment;
    private EditText etProjectDate;
    private EditText etProjectVoltageLevel;
    private EditText etProjectScheme;
    private EditText etProjectCarDistance;
    private EditText etProjectHandDistance;
    private EditText etProjectRemark;

    private GTaskPresenter gTaskPresenter;

    private ProjectBean projectBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_info);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        ActionBarUtil.setActionBar(this, R.string.project_info);

        Intent intent = getIntent();
        String projectId = intent.getStringExtra("projectId");
        if (projectId == null)
        {
            ToastUtils.toast(this,"项目ID获取失败");
            return;
        }
        else
        {
            this.projectBean = ((CSPGApplication) getApplication()).getDaoSession().getProjectBeanDao().queryBuilder()
                    .where(ProjectBeanDao.Properties.ProjectId.eq(projectId)).unique();
        }
        tvProjectName = findViewById(R.id.tv_project_info_project_name);
        tvProjectUnit = findViewById(R.id.tv_project_info_project_unit);
        etProjectCode = findViewById(R.id.tv_project_info_project_code);
        etProjectInvestment = findViewById(R.id.et_project_info_project_investment);
        etProjectDate = findViewById(R.id.et_project_info_project_date);
        etProjectVoltageLevel = findViewById(R.id.et_project_info_voltage_level);
        etProjectScheme = findViewById(R.id.et_project_info_design_model);
        etProjectCarDistance = findViewById(R.id.et_project_info_car_distance);
        etProjectHandDistance = findViewById(R.id.et_project_info_hand_distance);
        etProjectRemark = findViewById(R.id.et_project_info_remark);
        Button save = findViewById(R.id.btn_project_info_save);
        save.setOnClickListener(this);
        etProjectVoltageLevel.setOnClickListener(this);
        gTaskPresenter = new GTaskPresenter(this,null,null,null,null);
        initUI();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_project_info_save)
        {
            String voltageLevel = etProjectVoltageLevel.getText().toString();
            String carDistance = etProjectCarDistance.getText().toString();
            String handDistance = etProjectHandDistance.getText().toString();
            if (voltageLevel.trim().isEmpty())
            {
                ToastUtils.toast(this,"电压等级不能为空。。。");
            }
            else if (carDistance.trim().isEmpty())
            {
                ToastUtils.toast(this,"汽车运距不能为空....");
            }
            else  if(handDistance.trim().isEmpty())
            {
                ToastUtils.toast(this,"人力运距不能为空。。。。。");
            }
            if (projectBean == null)
            {
                ToastUtils.toast(this,"无项目数据。。。。");
                return;

            }
            projectBean.setVoltageLevel(voltageLevel);
            projectBean.setCarDistance(carDistance);
            projectBean.setHandDistance(handDistance);
            projectBean.setProjectInvestment(etProjectInvestment.getText().toString());
            projectBean.setProjectDate(etProjectDate.getText().toString());
            projectBean.setRemark(etProjectRemark.getText().toString());
            gTaskPresenter.updateProject(projectBean,3,0);//更新成功，什么也不做

        }
        else if (v.getId()==R.id.et_project_info_voltage_level)
        {
            voltageDialog();
        }
    }


    private void initUI()
    {
        tvProjectName.setText(projectBean.getProjectTitle());
        tvProjectUnit.setText(projectBean.getAffiliattedUnit()) ;
        etProjectCode.setText(projectBean.getProjectCode()) ;
        etProjectInvestment.setText(projectBean.getProjectInvestment()) ;
        etProjectDate.setText(projectBean.getProjectDate()) ;
        etProjectVoltageLevel.setText(projectBean.getVoltageLevel()) ;
        etProjectScheme.setText(projectBean.getSchemeName()) ;
        etProjectCarDistance.setText(projectBean.getCarDistance()) ;
        etProjectHandDistance.setText(projectBean.getHandDistance()) ;
        etProjectRemark.setText(projectBean.getRemark()) ;
        if (!UserInfo.getInstance().role.contains("设计"))
        {
            findViewById(R.id.ll_project_remark).setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 点击空白区域隐藏键盘.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (ProjectInfoActivity.this.getCurrentFocus() != null) {
                if (ProjectInfoActivity.this.getCurrentFocus().getWindowToken() != null) {
                    imm.hideSoftInputFromWindow(ProjectInfoActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
        return super.onTouchEvent(event);
    }

    public void voltageDialog()
    {
        String[] items = {"10KV", "380V", "220V"};
        new AlertListDialog.ListBuilder(this)
                .setDividerColorRes(R.color.color_divider)
                .setDividerHeightRes(R.dimen.dp1).setIcon(R.drawable.ic_info)
                .setTitle("电压等级")
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        etProjectVoltageLevel.setText(items[which]);
                    }
                })
                .create().show();
    }
}